﻿#pragma once
#include <iostream>
#include <string>
using namespace std;

template<class K, class V>
struct AVLTreeNode
{
	pair<K, V> _kv;
	AVLTreeNode<K, V>* _left;
	AVLTreeNode<K, V>* _right;
	AVLTreeNode<K, V>* _parent;

	int _bf; // balance factor

	AVLTreeNode(const pair<K, V>& pr)
		:_kv(pr)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		,_bf(0)
	{}
};

template <class K, class V>
class AVLTree
{
	typedef AVLTreeNode<K, V> Node;
private:
	void RotateL(Node* parent)
	{
		//左旋，parent->_bf = 2,parent->_right->_bf = 1 && RR 型
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL) subRL->_parent = parent;

		Node* pparent = parent->_parent;

		subR->_left = parent;
		parent->_parent = subR;

		if (_root == parent)
		{
			_root = subR;
		}
		else
		{
			if (pparent->_left == parent)
			{
				pparent->_left = subR;
			}
			else
			{
				pparent->_right = subR;
			}
		}

		subR->_bf = parent->_bf = 0;
	}

	void RotateR(Node* parent)
	{
		//右旋 parent->_bf=-2(_left's hight > _right's hight) && parent->_left->_bf = -1 LL型
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		//调整parent节点(1.指向谁 2.parent指向谁的_parent指针指向parent)
		//将parent->_left指向subL的右孩子
		parent->_left = subLR;//1.
		//判断subLR是否为空，当h == 0是subLR是空树
		if (subLR) subLR->_parent = parent;//2.

		//调整subL节点
		//将subL->_right指向parent,并保存parent的_parent节点
		Node* pparent = parent->_parent;
		subL->_right = parent;//1.
		parent->_parent = subL;//2.
		if (parent == _root)//3.subL指针指向谁
		{
			subL->_parent = nullptr;
			_root = subL;
		}
		else
		{
			if (pparent->_left == parent)
			{
				pparent->_left = subL;
			}
			else
			{
				pparent->_right = subL;
			}
		}
		//调整平衡因子
		subL->_bf = parent->_bf = 0;
	}

	void RotareLR(Node* parent)
	{

	}

	void In_Order(Node* root)
	{
		if (root == nullptr)
			return;
		In_Order(root->_left);
		cout << root->_kv->first << " ： " << root->_kv->second << endl;
		In_Order(root->_right);
	}
	
	Node* _root;
public:
	AVLTree()
		:_root(nullptr)
	{}

	void print()
	{
		In_Order(_root);
	}

	bool Insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}

		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (kv.first > cur->_kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (kv.first < cur->_kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}

		cur = new Node(kv);
		if (kv.first > parent->_kv.first)
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}

		cur->_parent = parent;

		//平衡因子(右子树减去左子树) 
		while (parent)
		{
			//更新平衡因子  
			if (parent->_left = cur)
			{
				parent->_bf--;
			}
			else
			{
				parent->_bf++;
			}
			//检查平衡因子
			if (parent->_bf == 0)
			{
				break;
			}
			else if (parent->_bf == -1 || parent->_bf == 1) //
			{
				cur = parent;
				parent = parent->_parent;
			}
			else
			{
				if (parent->_bf == -2 && parent->_left->_bf == -1)
				{
					RotateR(parent);
				}
				else if (parent->_bf == 2 && parent->_right->_bf == 1)
				{
					RotateL(parent);
				}
				else if (parent->_bf == -2 && parent->_left->_bf == 1)
				{
					RotateL(parent->_left);
					RotateR(parent);
				}
				else
				{
					RotateR(parent->_right);
					RotateL(parent);
				}
			}
		}
	}
};